﻿using DigitalHealth.PACS.ChatWebSocket.Repositories;
using DigitalHealth.PACS.ChatWebSocket.Repositories.Imp;
using DigitalHealth.PACS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DigitalHealth.PACS.ChatWebSocket.Services.Imp
{

    internal class ChatServerImp : IChatServer
    {
        public IChatMessageRepository ChatMessageRepository { get; set; }
        public IChatGroupMessageRepository ChatGroupMessageRepository { get; set; }
        public IChatUserRepository ChatUserRepository { get; set; }
        public IChatGroupRepository ChatGroupRepository { get; set; }

        public ChatServerImp(IChatMessageRepository chatMessageRepository, IChatGroupMessageRepository chatGroupMessageRepository, IChatUserRepository chatUserRepository, IChatGroupRepository chatGroupRepository)
        {
            ChatMessageRepository = chatMessageRepository;
            ChatGroupMessageRepository = chatGroupMessageRepository;
            ChatUserRepository = chatUserRepository;
            ChatGroupRepository = chatGroupRepository;
        }

        public bool ChatMessageExists(ChatServerContext chatServerContext)
        {
            if (chatServerContext.ChatType==Model.Enumeration.ChatType.私聊)
            {
                return ChatMessageRepository.ChatMessageExists(chatServerContext.ChatMessage.Id.ToString());
            }
            else if (chatServerContext.ChatType == Model.Enumeration.ChatType.群聊)
            {
                return ChatGroupMessageRepository.ChatMessageExists(chatServerContext.ChatMessage.Id.ToString());
            }
            return false;
        }

        public void Create(ChatServerContext chatServerContext)
        {
            if (chatServerContext.ChatType == Model.Enumeration.ChatType.私聊)
            {
                var chatMessage=(ChatMessage)chatServerContext.ChatMessage;
                //var senderUser = ChatUserRepository.GetChatUserById(chatMessage.SenderUser.Id.ToString());
                //var receiverUser = ChatUserRepository.GetChatUserById(chatMessage.ReceiverUser.Id.ToString());
                //if (senderUser==null|| receiverUser==null)
                //{
                //    return;
                //}
                //chatMessage.SenderUser = senderUser;
                //chatMessage.ReceiverUser = receiverUser;    
                ChatMessageRepository.Create(chatMessage);
            }
            else if (chatServerContext.ChatType == Model.Enumeration.ChatType.群聊)
            {
                ChatGroupMessageRepository.Create((ChatGroupMessage)chatServerContext.ChatMessage);
            }
        }

        public void Delete(ChatServerContext chatServerContext)
        {
            if (chatServerContext.ChatType == Model.Enumeration.ChatType.私聊)
            {
                ChatMessageRepository.Delete((ChatMessage)chatServerContext.ChatMessage);
            }
            else if (chatServerContext.ChatType == Model.Enumeration.ChatType.群聊)
            {
                ChatGroupMessageRepository.Delete((ChatGroupMessage)chatServerContext.ChatMessage);
            }
        }

        public IEnumerable<MsgTemplate> GetChatMessages(ChatServerContext chatServerContext)
        {
            if (chatServerContext.ChatType == Model.Enumeration.ChatType.私聊)
            {
                return ChatMessageRepository.GetChatMessagesByReceiverUser(((ChatMessage)chatServerContext.ChatMessage).ReceiverUser);
            }
            else if (chatServerContext.ChatType == Model.Enumeration.ChatType.群聊)
            {
                return ChatGroupMessageRepository.GetChatMessagesByReceiverOrg(((ChatGroupMessage)chatServerContext.ChatMessage).ReceiverOrg);
            }
            return null;
        }

        public IEnumerable<MsgTemplate> GetNotReadChatMessages(ChatServerContext chatServerContext)
        {
            if (chatServerContext.ChatType == Model.Enumeration.ChatType.私聊)
            {
                return ChatMessageRepository.GetNotReadChatMessagesByReceiverUser(((ChatMessage)chatServerContext.ChatMessage).ReceiverUser);
            }
            else if (chatServerContext.ChatType == Model.Enumeration.ChatType.群聊)
            {
                return ChatGroupMessageRepository.GetNotReadChatMessagesByReceiverOrg(((ChatGroupMessage)chatServerContext.ChatMessage).ReceiverOrg);
            }
            return null;
        }

        public void Update(ChatServerContext chatServerContext)
        {
            if (chatServerContext.ChatType == Model.Enumeration.ChatType.私聊)
            {
                ChatMessageRepository.Update((ChatMessage)chatServerContext.ChatMessage);
            }
            else if (chatServerContext.ChatType == Model.Enumeration.ChatType.群聊)
            {
                ChatGroupMessageRepository.Update((ChatGroupMessage)chatServerContext.ChatMessage);
            }
        }

        public IEnumerable<MsgTemplate> GetHistoryChatMessage(string receiverUserId)
        {
            var chatUser=ChatUserRepository.GetChatUserById(receiverUserId);
            return ChatMessageRepository.GetChatMessagesByReceiverUser(chatUser);
        }

        public IEnumerable<MsgTemplate> GetGroupHistoryChatMessage(string groupId)
        {
            var chatGroup = ChatGroupRepository.GetChatGroupById(groupId);
            return ChatGroupMessageRepository.GetChatMessagesByReceiverOrg(chatGroup);
        }

        public IEnumerable<MsgTemplate> GetNotReadChatMessage(string receiverUserId)
        {
            var user = ChatUserRepository.GetChatUserById(receiverUserId);
            return ChatMessageRepository.GetNotReadChatMessagesByReceiverUser(user);
        }

        public IEnumerable<MsgTemplate> GetNotReadGroupChatMessage(string receiverUserId)
        {
            List<ChatGroupMessage> chatGroupMessages = new List<ChatGroupMessage>();
            var user = ChatUserRepository.GetChatUserById(receiverUserId);
            var chatGroups=ChatGroupRepository.GetChatGroupByChatUser(user);
            foreach (var chatGroup in chatGroups)
            {
                var chatGroupMessage=ChatGroupMessageRepository.GetNotReadChatMessagesByReceiverOrg(chatGroup);
                chatGroupMessages.AddRange(chatGroupMessage);
            }
            return chatGroupMessages;
        }
    }
}
